package com.cq.controller;

import com.cq.pojo.Building;
import com.cq.service.BuildService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/student")
public class BuildingController {

    @Autowired
    private BuildService buildService;

    @RequestMapping("/queryAllBuilding")
    public String queryBuildName(ModelMap modelmap){
        List<Building> buildings = buildService.selectAllBuildName();
        modelmap.put("buildings",buildings);
        return "building/buildingManage";
    }

    @RequestMapping(value = "/addBuilding",method = RequestMethod.POST)
    public String addBuilding(HttpServletResponse resp,String build_id, String build_name, String room_amount, String floor_amount) throws IOException {
        int result = buildService.addBuilding(new Building(Integer.parseInt(build_id),build_name,
                Integer.parseInt(room_amount),Integer.parseInt(floor_amount)));
        PrintWriter out = resp.getWriter();
        if(result>0){
            out.flush();
            out.print("ok");
            out.close();
        }else {
            out.flush();
            out.print("fail");
            out.close();
        }
        return "building/buildingManage";
    }

    @RequestMapping(value = "/editBuilding",method = RequestMethod.POST)
    public String editBuilding(HttpServletResponse resp,String id, String name, String rooms, String floors) throws IOException {
        int result = buildService.editBuilding(new Building(Integer.parseInt(id),name,
                Integer.parseInt(rooms),Integer.parseInt(floors)));
        PrintWriter out = resp.getWriter();
        if(result>0){
            out.flush();
            out.print("ok");
            out.close();
        }else {
            out.flush();
            out.print("fail");
            out.close();
        }
        return "building/buildingManage";
    }

    @RequestMapping(value = "/delBuilding",method = RequestMethod.POST)
    public String delBuilding(HttpServletResponse resp,String id) throws IOException {
        int result = buildService.delBuilding(Integer.parseInt(id));
        PrintWriter out = resp.getWriter();
        if(result>0){
            out.flush();
            out.print("ok");
            out.close();
        }else {
            out.flush();
            out.print("fail");
            out.close();
        }
        return "building/buildingManage";
    }
}
